<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="下单日期" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="platformOrderNo">
        <el-input
          v-model="queryParams.platformOrderNo"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="蜜蜂汇云单号" prop="externalOrderNo" label-width="120">
        <el-input
          v-model="queryParams.externalOrderNo"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="brandName">
        <el-input
          v-model="queryParams.brandName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          clearable
          @change="handleQuery"
        >
          <el-option label="全部" :value="null"/>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="syncCardStatus"
          :disabled="selectedRows.length === 0"
        >
          同步状态
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          @click="dropOrder"
          :disabled="selectedRows.length === 0"
        >
          作废
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column label="订单号" align="center" prop="platformOrderNo" width="180"/>
      <el-table-column label="蜜蜂汇云单号" align="center" prop="externalOrderNo" />
      <el-table-column label="售价" align="center" prop="salePrice" width="80" />
      <el-table-column label="成本" align="center" prop="costPrice" width="80" />
      <el-table-column label="利润" align="center" prop="profit" width="80" />
      <el-table-column label="商品编码" align="center" prop="externalGoodsCode" />
      <el-table-column label="商品名称" align="center" prop="brandName" />
      <el-table-column label="取餐码" align="center" prop="pickUpRemark" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="链接状态" align="center" prop="isCardDrop">
        <template v-slot="{ row }">
          <span v-if="row.cardUsed">已使用</span>
          <span v-else-if="row.isCardDrop">已作废</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="是否使用" align="center" prop="cardUsed">
        <template v-slot="{row}">
          <span>{{ row.cardUsed ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单链接" align="center" prop="cardPwd" show-overflow-tooltip>
        <template v-slot="{row}">
          <a target="_blank" :href="row.cardPwd">{{ row.cardPwd }}</a>
        </template>
      </el-table-column>
      <el-table-column label="下单请求" align="center" prop="rawRequest">
        <template v-slot="{ row }">
          <div @click="previewJson(row.notifyResponse)" style="cursor: pointer; width: 100%; overflow:hidden; text-overflow: ellipsis; white-space: nowrap">{{ row.rawRequest }}</div>
        </template>
      </el-table-column>
      <el-table-column label="通知结果" align="center" prop="notifyResponse">
        <template v-slot="{ row }">
          <div @click="previewJson(row.notifyResponse)" style="cursor: pointer; width: 100%; overflow:hidden; text-overflow: ellipsis; white-space: nowrap">{{ row.notifyResponse }}</div>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="使用时间" align="center" prop="cardUsedTime" width="120"/>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, syncCardStatus, dropOrder } from "@/api/mf178/order"

export default {
  name: "Order",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      selectedRows: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // mf178表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        dateRange: null,
        platformOrderNo: null,
        externalOrderNo: null,
        brandName: null,
        orderStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询mf178列表 */
    getList() {
      const { dateRange, ...queryParams } = this.queryParams
      this.loading = true;
      listOrder({ ...queryParams, startTime: dateRange?.[0], endTime: dateRange?.[1] }).then(response => {
        this.orderList = response.rows
        this.total = response.total
        this.selectedRows = []
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        platformOrderNo: null,
        externalOrderNo: null,
        supplierOrderNo: null,
        appKey: null,
        customerCode: null,
        customerName: null,
        salePrice: null,
        costPrice: null,
        profit: null,
        orderSummary: null,
        platformGoodsName: null,
        externalGoodsName: null,
        supplierGoodsName: null,
        platformGoodsId: null,
        externalGoodsId: null,
        supplierGoodsId: null,
        platformGoodsCode: null,
        externalGoodsCode: null,
        supplierGoodsCode: null,
        brandName: null,
        orderStatus: null,
        subOrderStatus: null,
        cardType: null,
        cardNo: null,
        cardPwd: null,
        cardTime: null,
        cardExtendInfo: null,
        rawRequest: null,
        requestIp: null,
        refundTime: null,
        refundPrice: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selectedRows) {
      this.selectedRows = selectedRows;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加mf178"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改mf178"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除mf178编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mf178/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    previewJson(data) {
      const content = JSON.stringify(JSON.parse(data), null, 4);
      this.$msgbox({
        title: '预览',
        message: <div><pre style={{ whiteSpace: 'pre-wrap' }}>{ content }</pre></div>
      })
    },
    async syncCardStatus() {
      await this.$confirm('确定同步选中订单？');
      const loading = this.$loading();
      try {
        await syncCardStatus(this.selectedRows);
        this.$message.success('操作成功');
        this.getList();
      }finally {
        loading.close();
      }
    },
    async dropOrder() {
      await this.$confirm('确定作废选中订单？');
      const loading = this.$loading();
      try {
        await dropOrder(this.selectedRows);
        this.$message.success('操作成功');
        this.getList();
      }finally {
        loading.close();
      }
    },
  }
}
</script>
